<?php

/**
 * movie actions.
 *
 * @package    phpmymovies
 * @subpackage movie
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class movieActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->movie_list = MoviePeer::doSelect(new Criteria());
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MovieForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
	if ($request->hasParameter('imdbid'))
	{
		$movieimdb = new imdb ($request->getParameter('imdbid'));
		$movie = new Movie();
		$movie->setName($movieimdb->title());
		$movie->setGenre($movieimdb->genre());
		$movie->setPlot($movieimdb->plotoutline());
		$movie->setPoster($movieimdb->photo());
		$movie->setTime($movieimdb->runtime());		
		$movie->save();
		return $this->redirect('view/index');
	}
	else
	{
		$this->forward404Unless($request->isMethod('post'));
		$this->form = new MovieForm();
		$this->processForm($request, $this->form);
		$this->setTemplate('new');
	}
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($movie = MoviePeer::retrieveByPk($request->getParameter('id')), sprintf('Object movie does not exist (%s).', $request->getParameter('id')));
    $this->form = new MovieForm($movie);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($movie = MoviePeer::retrieveByPk($request->getParameter('id')), sprintf('Object movie does not exist (%s).', $request->getParameter('id')));
    $this->form = new MovieForm($movie);
    $this->processForm($request, $this->form);
    return $this->redirect('view/index');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($movie = MoviePeer::retrieveByPk($request->getParameter('id')), sprintf('Object movie does not exist (%s).', $request->getParameter('id')));
    $movie->delete();

    $this->redirect('movie/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $movie = $form->save();

      $this->redirect('view/index');
    }
  }
  
  public function executeSearchfromimdb(sfWebRequest $request)
  {
	$search = new imdbsearch();
	$search->search_episodes(FALSE);
	$search->setsearchname($request->getParameter('name'));
	$this->results = $search->results();
  }
}
